starter.controller('refundController', function($rootScope,$state,$ionicPopup,$timeout,$scope,Page, localStore,$location) {

    var orderId = $state.params.orderId;
    if(orderId == undefined || orderId == "") {
        Page.notice("非法请求!");
        return;
    }

    $scope.backInfos = new Map();

    $scope.screenWidth = document.body.clientWidth;
    $scope.screenHeight = window.innerHeight;

    Page.ajaxPost("app/order/canBackDetails.do",{orderId : orderId},function(resp){
        $scope.order = resp.data;
    });

    $scope.clickRefundData = function(passenger,date,index,$event) {
        var passengerObj = $scope.backInfos.get(passenger.passengerPhone);
        if(passengerObj == undefined) {
            passengerObj = {};
            passengerObj.passengerPhone = passenger.passengerPhone;
            passengerObj.passengerName = passenger.passengerName;
            $scope.backInfos.put(passengerObj.passengerPhone,passengerObj);
        }
        if($($event.currentTarget).css("background-color") == "rgb(169, 169, 169)") { //选中
            $($event.currentTarget).css("background-color","goldenrod");
            if(passengerObj.dates == undefined) {
                passengerObj.dates = [];
                var dateObj = {};
                dateObj.date = date.date;
                passengerObj.dates.push(dateObj);
            } else {
                var dateObj = {};
                dateObj.date = date.date;
                passengerObj.dates.push(dateObj);
            }
        } else { //取消选中
            for (var i = 0; i < passengerObj.dates.length; i++) {
                var obj = passengerObj.dates[i];
                if(obj.date == date.date) {
                    passengerObj.dates.splice(i,1);
                }
            }
            $($event.currentTarget).css("background-color","darkgray");
        }
    };

    $scope.refundApply = function() {
        //console.log($scope.backInfos.elements);
        var backInfos = [];
        if($scope.order.plan.planStatus == "1") {
            for (var i = 0; i < $scope.backInfos.elements.length; i++) {
                backInfos.push($scope.backInfos.elements[i].value);
            }
        } else {
            backInfos = angular.extend([],$scope.order.passengers);
        }
        var canApplyFlag = false;
        for (var i = 0; i < backInfos.length; i++) {
            var obj = backInfos[i];
            //只要有一个数组中包含日期就可以退款
            if(obj.dates.length > 0) {
                canApplyFlag = true;
                break;
            } else {
                canApplyFlag = false;
                continue;
            }
        }
        if(!canApplyFlag) {
            Page.notice("请选择退订日期!");
            return;
        }
        Page.ajaxPost("app/orderBack/getCanRefundAmount.do",{orderId : $scope.order.orderId,
            backInfos:backInfos},function(resp){
            var price = resp.data.price;
            Page.confirm("退款金额为:" + price + "￥,确定要退款吗?", function () {
                Page.ajaxPost("app/orderBack/apply.do",{orderId : $scope.order.orderId,orderBackType : '1',
                    backInfos:backInfos},function(resp){
                    $state.go("orders");
                    Page.notice("已发送退款申请!请等管理员通过!");
                });
            });
        });
    }

});